/* Includes ------------------------------------------------------------------*/
#include "flash.h"
#include "main.h"

/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
/*****************************************************
**\brief      读取 FLASH 数据
**\param[out] ret:	是否更新 FLASH 数据
*****************************************************/
uint8_t ReadFromFlash(void)
{
	uint8_t ret = 0;
	
	memcpy((uint16_t *)&fls_val, (const uint16_t *)APP_Var_Addr, sizeof(Varialble_TypeDef));
	if(fls_val.versions != FIRMWARE_VERSION){
    memset(&fls_val, 0x00, sizeof(Varialble_TypeDef));
		ret = 1;
	}
	
	return ret;
}

/*****************************************************
**\brief      写入 FLASH 数据
*****************************************************/
void WriteToFlash(void)
{
  uint32_t tmpVal;
  uint8_t *p;
  uint16_t i;
	
  fmc_unlock();
  
  fmc_page_erase(APP_Var_Addr);
  
  p = (uint8_t *)&fls_val;
  for(i=0; i<sizeof(Varialble_TypeDef); i+=4,p+=4){
    memcpy(&tmpVal, p, 4);
    fmc_word_program(APP_Var_Addr + i, tmpVal);
  }
  fmc_lock();
}

